/*
 * @author Robert Hanwell
 * @version 3
 * @since January
 */
package com.smartbox.floodaction;

import java.util.ArrayList;
import java.util.Arrays;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

// TODO: Auto-generated Javadoc
/**
 * The Class CreateEditGroupAction.
 */
public class CreateEditGroupAction extends BaseActivity implements
		OnItemSelectedListener {

	/** The plan name. */
	private EditText planName;

	/** The sp colour. */
	private Spinner spColour;

	/** The array adapter. */
	private ArrayAdapter<String> arrayAdapter;

	/** The colour list. */
	private ArrayList<String> colourList;

	/** The db helper. */
	private GroupActionData dbHelper;

	/** The m cur. */
	private Cursor mCur;

	/** The d. */
	private Dialog d;

	/** The colour items. */
	private String[] colourItems;

	/** The plan name value. */
	private String selectedColour, planNameValue;

	/** The row id. */
	private Long rowId;

	/** The get intent key. */
	private Bundle b, getIntentKey;

	/** The update flag. */
	private boolean updateFlag = false;

	/** The i. */
	private Intent i;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.creat_edit_group_action);

		getIntentKey = getIntent().getExtras();

		dbHelper = new GroupActionData(this);
		dbHelper.open();
		d = new Dialog(CreateEditGroupAction.this);

		initialize();
		addAdapter();

		if (getIntentKey.getLong("grpActId") > 0) {
			rowId = getIntentKey.getLong("grpActId");
			updateFlag = true;
			valuesToEdit();
		}

	}

	/**
	 * Values to edit.
	 */
	private void valuesToEdit() {
		mCur = dbHelper.getGroupActivtyColumnBy(rowId);
		startManagingCursor(mCur);
		if (mCur.moveToFirst()) {
			planName.setText(mCur.getString(mCur
					.getColumnIndexOrThrow(SQLHelper.KEY_ACT_PLAN_NAME)));

		}

	}

	// UI items
	/**
	 * Initialize.
	 */
	public void initialize() {
		planName = (EditText) findViewById(R.id.etPlanName);
		spColour = (Spinner) findViewById(R.id.spColour);
		spColour.setOnItemSelectedListener(this);
		b = new Bundle();
		i = new Intent();

	}

	// pop menu out
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.smartbox.floodaction.BaseActivity#onCreateOptionsMenu(android.view
	 * .Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater menuInf = getMenuInflater();
		if (updateFlag) {
			menuInf.inflate(R.menu.update_menu, menu);
		} else {
			menuInf.inflate(R.menu.submit_menu, menu);
		}

		return true;
	}

	/**
	 * Validate.
	 * 
	 * @return true, if successful
	 */
	public boolean validate() {
		planNameValue = planName.getText().toString();
		if (planNameValue.equalsIgnoreCase("")) {
			Toast.makeText(this, "Sorry you need to enter a Plan Name",
					Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}

	/**
	 * Gets the alert options.
	 * 
	 * @return the alert options
	 */
	public void getAlertOptions() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Group Activity Submited.")
				.setCancelable(false)
				.setPositiveButton("Enter New Group Action",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								planName.setText(null);
								dialog.cancel();
							}
						})
				.setNegativeButton("Enter Task",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								Intent i = new Intent(
										"com.smartbox.floodaction.CREATEEDITTASK");
								b.putLong("GRPPK", rowId);

								i.putExtras(b);
								startActivity(i);
							}

						})
				.setNeutralButton("Enter A Volunteer",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int id) {
								Intent i = new Intent(
										"com.smartbox.floodaction.CREATEEDITVOLUNTEER");
								b.putLong("GRPPK", rowId);
								i.putExtras(b);
								startActivity(i);

							}
						});
		AlertDialog alert = builder.create();
		alert.show();
	}

	/**
	 * Adds the adapter.
	 */
	public void addAdapter() {
		Resources aResource = this.getResources();
		colourItems = aResource.getStringArray(R.array.colorValues);
		colourList = new ArrayList<String>(Arrays.asList(colourItems));
		// this is the drop down list which has pickup the color from the
		// string.xml
		arrayAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, colourList);
		// this is the pop out list
		arrayAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spColour.setAdapter(arrayAdapter);

	}

	// hardkey menu options
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.smartbox.floodaction.BaseActivity#onOptionsItemSelected(android.view
	 * .MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.bUpdate:
			if (validate()) {
				try {
					dbHelper.updateGroupActionCategory(rowId, planNameValue,
							selectedColour);

				} catch (Exception e) {
					String error = e.toString();
					TextView tv = new TextView(this);
					tv.setText(error);
					d.setContentView(tv);
					d.show();
				} finally {
					i = new Intent("com.smartbox.floodaction.CAPTABBAR");
					startActivity(i);
					finish();
				}

			}
			return true;
		case R.id.bSubmit:
			// submit data
			if (validate()) {
				try {
					rowId = dbHelper.createEntryGroupActionCategory(
							planNameValue, selectedColour);
				} catch (Exception e) {
					String error = e.toString();
					TextView tv = new TextView(this);
					tv.setText(error);
					d.setContentView(tv);
					d.show();
				} finally {
					Toast.makeText(this, "rowid  " + rowId.toString(),
							Toast.LENGTH_LONG).show();
					getAlertOptions();
				}
			}
			return true;
		case R.id.bReset:

			return true;

		}

		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemSelectedListener#onItemSelected(android
	 * .widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		selectedColour = colourList.get(arg2);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(android
	 * .widget.AdapterView)
	 */
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	// after create start database connection
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStart()
	 */
	@Override
	protected void onStart() {
		super.onStart();
		Log.i("onStart", "open database");
		dbHelper.open();

	}

	// before we close the screen close the database
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		Log.i("onStop", "closing database");
		dbHelper.close();

	}

}
